2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_ResourceEditorPanel.h"
30 //==============================================================================
31 class ResourceListButton
: public Component
,
32 private ButtonListener
35 ResourceListButton (JucerDocument
& document_
)
36 : document (document_
)
38 setInterceptsMouseClicks (false, true);
39 addAndMakeVisible (reloadButton
= new TextButton ("Reload"));
40 reloadButton
->addListener (this);
48 void update (int newRow
, bool isSelected_
)
52 const BinaryResources::BinaryResource
* const r
= document
.getResources() [row
];
53 reloadButton
->setVisible (r
!= 0);
58 reloadButton
->setBoundsInset (BorderSize
<int> (2));
61 void buttonClicked (Button
*)
63 const BinaryResources::BinaryResource
* const r
= document
.getResources() [row
];
67 document
.getResources()
68 .browseForResource ("Select a file to replace this resource",
70 File (r
->originalFilename
),
76 JucerDocument
& document
;
79 TextButton
* reloadButton
;
83 //==============================================================================
84 ResourceEditorPanel::ResourceEditorPanel (JucerDocument
& document_
)
85 : document (document_
)
87 addAndMakeVisible (addButton
= new TextButton ("Add new resource..."));
88 addButton
->addListener (this);
90 addAndMakeVisible (reloadAllButton
= new TextButton ("Reload all resources"));
91 reloadAllButton
->addListener (this);
93 addAndMakeVisible (delButton
= new TextButton ("Delete selected resources"));
94 delButton
->addListener (this);
95 delButton
->setEnabled (false);
97 addAndMakeVisible (listBox
= new TableListBox (String::empty
, this));
98 listBox
->getHeader().addColumn ("name", 1, 150, 80, 400);
99 listBox
->getHeader().addColumn ("original file", 2, 350, 80, 800);
100 listBox
->getHeader().addColumn ("size", 3, 100, 40, 150);
101 listBox
->getHeader().addColumn ("reload", 4, 100, 100, 100, TableHeaderComponent::notResizableOrSortable
);
102 listBox
->getHeader().setStretchToFitActive (true);
104 listBox
->setColour (ListBox::outlineColourId
, Colours::darkgrey
);
105 listBox
->setOutlineThickness (1);
106 listBox
->updateContent();
108 document
.addChangeListener (this);
109 handleCommandMessage (1);
112 ResourceEditorPanel::~ResourceEditorPanel()
114 document
.removeChangeListener (this);
118 int ResourceEditorPanel::getNumRows()
120 return document
.getResources().size();
123 void ResourceEditorPanel::paintRowBackground (Graphics
& g
, int rowNumber
, int width
, int height
, bool rowIsSelected
)
126 g
.fillAll (findColour (TextEditor::highlightColourId
));
129 void ResourceEditorPanel::paintCell (Graphics
& g
, int rowNumber
, int columnId
, int width
, int height
, bool rowIsSelected
)
131 const BinaryResources::BinaryResource
* const r
= document
.getResources() [rowNumber
];
139 else if (columnId
== 2)
140 text
= r
->originalFilename
;
141 else if (columnId
== 3)
142 text
= File::descriptionOfSizeInBytes (r
->data
.getSize());
145 g
.drawText (text
, 4, 0, width
- 6, height
, Justification::centredLeft
, true);
149 Component
* ResourceEditorPanel::refreshComponentForCell (int rowNumber
, int columnId
, bool isRowSelected
, Component
* existingComponentToUpdate
)
154 if (existingComponentToUpdate
== 0)
155 existingComponentToUpdate
= new ResourceListButton (document
);
157 ((ResourceListButton
*) existingComponentToUpdate
)->update (rowNumber
, isRowSelected
);
159 return existingComponentToUpdate
;
162 int ResourceEditorPanel::getColumnAutoSizeWidth (int columnId
)
170 for (int i
= document
.getResources().size(); --i
>= 0;)
172 const BinaryResources::BinaryResource
* const r
= document
.getResources() [i
];
177 else if (columnId
== 2)
178 text
= r
->originalFilename
;
179 else if (columnId
== 3)
180 text
= File::descriptionOfSizeInBytes (r
->data
.getSize());
182 widest
= jmax (widest
, f
.getStringWidth (text
));
188 //==============================================================================
192 ResourceSorter (const int columnId_
, const bool forwards
)
193 : columnId (columnId_
),
194 direction (forwards
? 1 : -1)
198 int compareElements (BinaryResources::BinaryResource
* first
, BinaryResources::BinaryResource
* second
)
201 return direction
* first
->name
.compare (second
->name
);
202 else if (columnId
== 2)
203 return direction
* first
->originalFilename
.compare (second
->originalFilename
);
204 else if (columnId
== 3)
205 return direction
* (int) first
->data
.getSize() - (int) second
->data
.getSize();
211 const int columnId
, direction
;
212 ResourceSorter (const ResourceSorter
&);
213 ResourceSorter
& operator= (const ResourceSorter
&);
216 void ResourceEditorPanel::sortOrderChanged (int newSortColumnId
, const bool isForwards
)
218 ResourceSorter
sorter (newSortColumnId
, isForwards
);
219 document
.getResources().sort (sorter
);
222 //==============================================================================
223 void ResourceEditorPanel::selectedRowsChanged (int lastRowSelected
)
225 delButton
->setEnabled (listBox
->getNumSelectedRows() > 0);
228 void ResourceEditorPanel::resized()
230 listBox
->setBounds (6, 4, getWidth() - 12, getHeight() - 38);
232 addButton
->changeWidthToFitText (22);
233 addButton
->setTopLeftPosition (8, getHeight() - 30);
235 reloadAllButton
->changeWidthToFitText (22);
236 reloadAllButton
->setTopLeftPosition (addButton
->getRight() + 10, getHeight() - 30);
238 delButton
->changeWidthToFitText (22);
239 delButton
->setTopRightPosition (getWidth() - 8, getHeight() - 30);
242 void ResourceEditorPanel::visibilityChanged()
245 listBox
->updateContent();
248 void ResourceEditorPanel::changeListenerCallback (ChangeBroadcaster
*)
251 listBox
->updateContent();
254 void ResourceEditorPanel::buttonClicked (Button
* b
)
258 document
.getResources()
259 .browseForResource ("Select a file to add as a resource",
264 else if (b
== delButton
)
266 document
.getResources().remove (listBox
->getSelectedRow (0));
268 else if (b
== reloadAllButton
)
272 for (int i
= 0; i
< document
.getResources().size(); ++i
)
274 if (! document
.getResources().reload (i
))
275 failed
.add (document
.getResources().getResourceNames() [i
]);
278 if (failed
.size() > 0)
280 AlertWindow::showMessageBox (AlertWindow::WarningIcon
,
281 TRANS("Reloading resources"),
282 TRANS("The following resources couldn't be reloaded from their original files:\n\n")
283 + failed
.joinIntoString (", "));